package me.pplay.playtalk.protocol;

import java.util.LinkedList;

import me.pplay.playtalk.protocol.bean.SendNode;
import me.pplay.playtalk.util.LogManager;

public class ProtocolList {
	public static int s_session_id = 0;

	private LinkedList<SendNode> list = new LinkedList<SendNode>();

	private void tp_data_append_send_real(SendNode node, boolean isappend) {
		if (isappend)
			list.addLast(node);
		else
			list.addFirst(node);
	}

	public void tp_data_append_send(int cmd, int timeout, byte data[], DataCallBack callBack) {
		SendNode node = tp_data_get_sendnode(cmd, timeout, data, callBack);
		node.setSession_id(++s_session_id);
		tp_data_append_send_real(node, true);
	}

	public void tp_data_prepend_send(int cmd, int timeout, byte data[], DataCallBack callBack) {
		SendNode node = tp_data_get_sendnode(cmd, timeout, data, callBack);
		node.setSession_id(++s_session_id);
		tp_data_append_send_real(node, false);
	}

	public SendNode tp_data_get_sendnode(int cmd, int timeout, byte data[], DataCallBack callBack) {
		SendNode node = new SendNode();
		node.setCmd(cmd);
		node.setTimeout(timeout);
		node.setData(data);
		node.setCallBack(callBack);
		LogManager.LogShow("TpAuth", "tp_data_get_sendnode callBack"+callBack);
		LogManager.LogShow("TpAuth", "tp_data_get_sendnode callBack"+node.getCallBack());
		if (data != null)
			node.setDatalen(data.length);
		return node;
	}

	public boolean tp_data_remove_send(int cmd) {
		SendNode nodetmp = null;
		for (SendNode node : list) {
			if (node.getCmd() == cmd) {
				nodetmp = node;
				break;
			}
		}
		if (list!= null && nodetmp != null){
			list.remove(nodetmp);
			return true;
		}
		return false;
	}

	public void tp_data_removeall_send() {
		list.clear();
	}

	public boolean tp_data_findnode(int cmd) {
		for (SendNode node : list) {
			if (node.getCmd() == cmd) {
				return true;
			}
		}
		return false;
	}

	public SendNode tp_data_firstnode() {
		for (SendNode node : list) {
			return node;
		}
		return null;
	}
}
